package net.yeah.mickey_zhouyou.module.booklist.util;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTree;

import net.yeah.mickey_zhouyou.framework.frame.MainFrame;
import net.yeah.mickey_zhouyou.framework.util.StringUtil;
import net.yeah.mickey_zhouyou.module.booklist.biz.CategoryManager;
import net.yeah.mickey_zhouyou.module.booklist.biz.impl.CategoryManagerImpl;
import net.yeah.mickey_zhouyou.module.booklist.component.CategoryTreeNode;

public class PopupMenuFactory {

	private static CategoryManager cm = CategoryManagerImpl.getInstance();

	public static JPopupMenu getCategoryTreeBlankMenu(final JTree tree) {
		if (categoryTreeBlankMenu == null) {
			categoryTreeBlankMenu = new JPopupMenu();
			JMenuItem addRoot = new JMenuItem("添加树根", new ImageIcon(
					Constants.ICON_ADD_CATEGORY_NODE));
			categoryTreeBlankMenu.add(addRoot);
			addRoot.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					String name = JOptionPane.showInputDialog(
							MainFrame.getMainFrame(), "请输入名字");
					if (StringUtil.isNotEmpty(name))
						cm.addTreeRoot(tree, name);
				}
			});
		}
		return categoryTreeBlankMenu;
	}

	public static JPopupMenu getCategoryTreeNodeMenu(final JTree tree) {
		if (categoryTreeNodeMenu == null) {
			categoryTreeNodeMenu = new JPopupMenu();
			JMenuItem add = new JMenuItem("添加子节点", new ImageIcon(
					Constants.ICON_ADD_CATEGORY_NODE));
			JMenuItem edit = new JMenuItem("编辑此节点", new ImageIcon(
					Constants.ICON_EDIT_CATEGORY_NODE));
			JMenuItem del = new JMenuItem("删除此节点", new ImageIcon(
					Constants.ICON_DEL_CATEGORY_NODE));
			JMenuItem addRoot = new JMenuItem("添加树根");

			add.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					String name = JOptionPane.showInputDialog(
							MainFrame.getMainFrame(), "请输入名字");
					if (StringUtil.isNotEmpty(name))
						cm.addChild(tree, (CategoryTreeNode) tree
								.getLastSelectedPathComponent(), name);
				}
			});

			edit.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					CategoryTreeNode selectedNode = (CategoryTreeNode) tree
							.getLastSelectedPathComponent();
					String name = JOptionPane.showInputDialog(MainFrame
							.getMainFrame(), "请输入名字", selectedNode
							.getCategory().getText());
					if (StringUtil.isNotEmpty(name))
						cm.changeNodeText(tree, selectedNode, name);
				}
			});

			del.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					int selection = JOptionPane.showConfirmDialog(
							MainFrame.getMainFrame(), "确定要删除么", "?",
							JOptionPane.OK_CANCEL_OPTION);
					if (selection == JOptionPane.OK_OPTION)
						cm.delSelectedNode(tree);
				}
			});

			addRoot.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					String name = JOptionPane.showInputDialog(
							MainFrame.getMainFrame(), "请输入名字");
					if (StringUtil.isNotEmpty(name))
						cm.addTreeRoot(tree, name);
				}
			});

			categoryTreeNodeMenu.add(add);
			categoryTreeNodeMenu.add(edit);
			categoryTreeNodeMenu.add(del);
			categoryTreeNodeMenu.add(addRoot);
		}
		return categoryTreeNodeMenu;
	}

	private static JPopupMenu categoryTreeBlankMenu;
	private static JPopupMenu categoryTreeNodeMenu;

}
